package com.hanock.sm.fork2d;

import java.nio.ByteBuffer;


import android.graphics.Rect;

public class Texture {

	private int textureId;
	private int textureWidth;
	private int textureHeight;
	private float imageRight;
	private float imageBottom;
	
	
	public Texture(int textureId, int imageWidth, int imageHeight) {
		
		this.textureId = textureId;
		
		textureWidth = TextureUtil.getTextureLength(imageWidth);
		textureHeight = TextureUtil.getTextureLength(imageHeight);
		
		imageRight = imageWidth * 1f/ textureWidth;
		imageBottom = imageHeight * 1f / textureHeight;
	}
	
	/**
	 * @param rect : Rect is a portion of the original image represented by
	 *  			pixels and it uses coordinate system where the
	 *   			top-leftmost point's coordinate is (0,0)
	 *   
	 * @param coordinate :  
	 *  (left, bottom) -> (left, top) -> (right, bottom) -> (right, top)
	 */
	public void getTextureCoordinate(Rect rect, ByteBuffer coordinate){

		float left = rect.left * 1f / textureWidth;
		float right = rect.right * 1f / textureWidth;
		float top = rect.top * 1f / textureHeight;
		float bottom = rect.bottom * 1f / textureHeight;
		
		int pos = coordinate.position();
		
		coordinate.putFloat(left);
		coordinate.putFloat(bottom);
		
		coordinate.putFloat(left);
		coordinate.putFloat(top);
		
		coordinate.putFloat(right);
		coordinate.putFloat(bottom);
		
		coordinate.putFloat(right);
		coordinate.putFloat(top);
		
		coordinate.position(pos);
	}
	
	/**
	 * @param coordinate :  
	 *  (left, bottom) -> (left, top) -> (right, bottom) -> (right, top)
	 */
	public void getTextureCoordinate(ByteBuffer coordinate){
		
		float left = 0.0f;
		float right = imageRight;
		float top = 0.0f;
		float bottom = imageBottom;
		
		int pos = coordinate.position();
		
		coordinate.putFloat(left);
		coordinate.putFloat(bottom);
		
		coordinate.putFloat(left);
		coordinate.putFloat(top);
		
		coordinate.putFloat(right);
		coordinate.putFloat(bottom);
		
		coordinate.putFloat(right);
		coordinate.putFloat(top);
		
		coordinate.position(pos);
	}

	public int getTextureId(){
		return textureId;
	}

	public int getTextureWidth() {
		return textureWidth;
	}
	
	public int getTextureHeight() {
		return textureHeight;
	}
	
	public int getImageWidth() {
		return (int)(textureWidth*imageRight);
	}
	
	public int getImageHeight() {
		return (int)(textureHeight*imageBottom);
	}

}











